package com.leyou.item.web;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.pojo.Item;
import com.leyou.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController  //声明当前类是控制层，服务请求相当于Servlet
@RequestMapping("item")  //设置其对应的访问的映射名称，也就是当前Servlet的别名
public class itemController {
	//注入Service
	@Autowired
	private ItemService itemService;
	@PostMapping  //拦截post请求，可以设置其对应的映射方法的路径
	public ResponseEntity<Item> saveItem(Item item){//ResponseEntity<Item>设置返回状态码类型
		//业务逻辑做校验
		//校验价格
		if(item.getPrice() == null){
			//请求失败返回状态码400，设置其内容是空
			throw new LyException(ExceptionEnum.PRICE_CANNOT_BE_NULL);
		}
		item = itemService.saveItem(item);
		//设置返回的是状态码类型的是201以及其类型是item类型
		return ResponseEntity.status(HttpStatus.CREATED).body(item);
	}
}
